// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueMaterial from 'vue-material'
import 'vue-material/dist/vue-material.min.css'
import 'vue-material/dist/theme/default.css' // This line here
import global_ from './util/const/global'// 引用全局变量，整个应用都有效
import {configDb} from '@/util/dao/ConfigDB'
// import {GoogleCharts} from 'google-charts'
// 由于VueMaterial没有DateTimePicker，所以我选择muse-ui
import { DateInput } from 'muse-ui'
import ElementUI from 'element-ui'
import {formateDate} from '@/util/ApplicationUtil'

Vue.prototype.GLOBAL = global_ // 挂载到Vue实例上面
// Vue.prototype.$GoogleCharts = GoogleCharts
Vue.use(VueMaterial)
Vue.use(DateInput)
Vue.use(ElementUI)
Vue.config.productionTip = false

// 配置日期格式化，在所有需要规定日期格式的地方调用formdate方法
formateDate()
// 配置数据库
configDb()
/* 注册service-worker */
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('service-worker.js').then(registration => {
      console.log('SW registered: ', registration)
    }).catch(registrationError => {
      console.log('SW registration failed: ', registrationError)
    })
  })
}
/**
 * 登陆控制
 */
router.beforeEach((to, from, next) => {
  // 判断本地是否存有用户的登陆信息，没有就调到登陆页面，否则就取首页
  let token = localStorage.getItem('token')
  if (to.meta.requiresAuth === true && token === null) {
    /* 跳转到登陆页面 */
    next({path: 'login'})
  } else {
    /* 跳转到首页 */
    // 设置vuex登录状态为已登录
    // store.commit('setLogin', {isLogin: true}),取消了
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {App}
})
